package com.zengqingfa.interview;

public class Add implements Runnable {

    int start;
    private static int sum = 0;

    public Add(int start) {
        this.start = start;
    }

    @Override
    public void run() {
        this.add(start);
    }

    public synchronized void add(int start) { //加锁，避免线程不安全
        /*
         * 或者 Lock lock = new ReentrantLock();
         *  lock.lock();
         */
        for (int i = start; i < start + 10; i++) {
            sum += i;
        }
        System.out.println(Thread.currentThread().getName()+"计算的结果："+sum);
        // lock.unlock;
    }

    public static int getSum() {
        return sum;
    }
}

